package com.tjsiqi.controller;

import com.tjsiqi.beans.PageQuery;
import com.tjsiqi.common.JsonData;
import com.tjsiqi.model.WxhntRWDEXP;
import com.tjsiqi.param.SearchstRwdParam;
import com.tjsiqi.param.SearchwxUnitsParam;
import com.tjsiqi.service.SysStRwdService;
import com.tjsiqi.util.DateTimeUtil;
import com.tjsiqi.util.ExportExcelUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * Created by xxlc
 */

@Controller
@RequestMapping("/sys/stRwd")
public class SysstRwdController {

    @Resource
    private SysStRwdService sysstRwdService;

    //商砼任务单统计
    @RequestMapping("/StRwdTJ.page")
    public ModelAndView TJpage() {
        return new ModelAndView("StRwdTJ");
    }

    @RequestMapping("/StRwdTJ1.page")
    public ModelAndView TJ1page() {
        return new ModelAndView("LqRwdTJ");
    }

    //商砼任务单明细
    @RequestMapping("/stRwd.page")
    public ModelAndView page() {
        return new ModelAndView("strwd");
    }

    //管理员查询未审核商砼任务单明细页面
    @RequestMapping("/stRwdByManager.page")
    public ModelAndView pageByManager() {
        return new ModelAndView("strwdByManager");
    }

// 更新任务单
    @RequestMapping("/updateById")
    @ResponseBody
    public JsonData acceptById(@RequestParam("id") int id, @RequestParam("status") String status) {
        sysstRwdService.acceptById(id, status);
        return JsonData.success();
    }

//  查询任务单数据
    @RequestMapping("/page.json")
    @ResponseBody
    public JsonData searchPage(HttpServletRequest request, HttpServletResponse response, SearchstRwdParam param, PageQuery page) {
        return JsonData.success(sysstRwdService.searchPageList(param, page));
    }


//  导出商砼任务单
    @RequestMapping("/export")
    @ResponseBody
    public JsonData exportByParam(HttpServletRequest request, HttpServletResponse response, SearchstRwdParam param) throws IOException {
        String filename = "商砼任务单" + DateTimeUtil.dateToStr(new Date(), "yyyy-MM-dd");

        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".xls");
        response.setContentType("application/x-download");
        ExportExcelUtil<WxhntRWDEXP> util = new ExportExcelUtil<WxhntRWDEXP>();
        param.setStUnit(new String(param.getStUnit().getBytes("iso-8859-1"), "utf-8"));
        List<WxhntRWDEXP> stRwdList = sysstRwdService.exportByParam(param);
        OutputStream ouputStream = response.getOutputStream();
        String[] columnNames = {"编号", "施工单位", "工程名称", "工程地址", "联系人", "联系电话", "供货日期", "供货时间", "浇筑方式", "泵车台数", "臂长", "周边环境", "浇筑部位", "强度等级", "抗渗等级", "抗折等级", "防腐等级", "阻锈要求", "其他要求", "坍落度(mm)", "订货数量(m³)", "备注", "订单状态", "下单时间"};
        util.exportExcel("商砼任务单列表", columnNames, stRwdList, ouputStream, ExportExcelUtil.EXCEL_FILE_2003);
        return JsonData.success();
    }

//    删除任务单
    @RequestMapping("/remove")
    @ResponseBody
    public JsonData removeByParam(HttpServletRequest request, HttpServletResponse response, SearchstRwdParam param)  {
        System.out.println("ceshishifouremove"+StringUtils.isBlank(param.getJbzCode()));
        if (StringUtils.isBlank(param.getJbzCode())) {
            return JsonData.fail("搅拌站编号获取失败，请刷新重试！");
        } else{
            int isRemove = sysstRwdService.removeByParam(param);
            return JsonData.success(isRemove);
        }
    }


}
